<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$errors = array();

if (isset($_POST['action']) && $_POST['action'] == 'add')
{
	$city = UV::readInt($route->POST("city"));
	$link = UV::readMQ($route->POST("link"));
	$head_office = UV::readInt($route->POST("head_office"));
	$name = UV::readMQ($route->POST("name"));
	$address = UV::readMQ($route->POST("address"));
	$phone = UV::readMQ($route->POST("phone"));
	$about = UV::readMQ($route->POST("about"));
	$reliability = UV::readInt($route->POST("reliability"));
	$order = UV::readInt($route->POST("order"));
	$donor = trim(UV::readMQ($route->POST("donor")));
	$cookie = trim(UV::readMQ($route->POST("cookie")));

	$query = "insert into `companies` values(NULL,'$city','$link','$head_office','$name','$address','$phone','$about','$reliability','$order','$donor','$cookie')";
	
	$update = $db->sendQuery($query);
	if ($update){
		$company_id = mysql_insert_id();
		
		$services = array();
		if ($route->POST("services")){
			foreach ($route->POST("services") as $s){
				if (!in_array($s,$services)){
					$services[] = $s;
				}
			}
		}
		
		if (count($services) > 0){
			$query = "insert into `providers` values ";
			foreach ($services as $key=>$service){
				$query .= "(NULL,'$company_id','$service')";
				if ($key < count($services) - 1)
					$query .= ",";
			}
			
			if (!$db->sendQuery($query))
				$errors[] = "Ошибка добавления услуг";
		}
	} else {
		$errors[] = "Ошибка добавления записи";
	}
	
	if (count($errors) == 0){
		goToPage("/admin/companies/edit/$company_id");
	}
}

loadPage("head.php");
?>
<script type='text/javascript'>
$(document).ready(function(){
	$("#name").keyup(function(){
		$("#link").val(ru2en.translit($("#name").val()));
	})
	$("#add_service").click(function(){
		var select = "<tr><td><select name='services[]' class='double_short'>"+$("#hidden_select").html()+"</select></td><td><input type='button' class='del' value='Удалить' /></td></tr>";
		$(this).parents("tr:eq(0)").before(select);
	})
	$(".del").live("click",function(){
		var parent = $(this).parents("tr:eq(0)");
		parent.css("background","#faa");
		var answer = confirm("Действительно удалить?");
		parent.css("background","");
		if (answer){
			parent.remove();
		}
	})
	
	// load head office data
	$("#head_office").change(function(){
		var id = $(this).val();
		if (id != 0) {
			$.ajax({
				url: "/ajax/headofficedata",
				data: "id="+id,
				type: "post",
				success: function(data){
					var head = $.parseJSON(data);
					$("#name").val(head.name.replace(/\\r/g,"\r").replace(/\\n/g,"\n"));
					$("#link").val(head.link);
					$("#about").val(head.about.replace(/\\r/g,"\r").replace(/\\n/g,"\n"));
					$("#reliability").val(head.reliability);
					$("#order").val(head.order);
					
					var options = $("#hidden_select option");
					$("#services_list tr:has(option)").remove();
					for (var i in head.services){
						var select = "<tr><td><select name='services[]' class='double_short'>";
						$(options).each(function(){
							select += "<option value='"+$(this).val()+"'";
							select += ($(this).val() == head.services[i]) ? "selected='selected'" : "";
							select += ">"+$(this).text()+"</option>";
						})
						select += "</select></td><td><input type='button' class='del' value='Удалить' /></td></tr>";
						$("#services_list").find("tr:last").before(select);
					}
				}
			})
		}
	})
})

</script>
<h1>Добавление компании</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
?>
<form method='POST' id='edit'>
	<input type='hidden' name='action' value='add' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'><label for='name'>Название</label></td>
			<td width='600px'><input type='text' id='name' name='name' value='<?php echo $route->POST('name'); ?>' /></td>
		</tr>
		<tr>
			<td><label for='link'>Ссылка</label></td>
			<td><input type='text' id='link' name='link' value='<?php echo $route->POST('link'); ?>' /></td>
		</tr>
		<tr>
			<td><label for='head_office'>Филиал</label></td>
			<td>
				<select class='double_short' id='head_office' name='head_office'>
					<option value='0'>Головной офис</option>
					<option disabled='disabled'>———————————————</option><?php
					$head_companies = $db->getTable("select `company_id`,`name` from `companies` where `head_office` = '0' order by `name`");
					echo select_create($head_companies,'company_id','name',$route->POST('head_office'));
				?></select>
			</td>
		</tr>
		<tr>
			<td><label for='city'>Город</label></td>
			<td>
				<select name='city' id='city' class='double_short'><?php
					$cities_list = $db->getTable("select `city_id`,`name` from `city` order by `name`");
					echo select_create($cities_list,"city_id","name",$route->POST('city_id'));
				?></select>
			</td>
		</tr>
		<tr>
			<td><label for='address'>Адрес</label></td>
			<td><input type='text' name='address' id='address' value='<?php echo $route->POST('address'); ?>' /></td>
		</tr>
		<tr>
			<td><label for='phone'>Телефон(ы)</label></td>
			<td><input type='text' name='phone' id='phone' value='<?php echo $route->POST('phone'); ?>' /></td>
		</tr>
		<tr>
			<td><label for='reliability'>Надежность</label></td>
			<td>
				<select name='reliability' id='reliability' class='short'>
					<option value='0'>Нет</option><?php
					$reliability_list = $db->getTable("select `reliability_id`,`class` from `reliability` order by `reliability_id`");
					echo select_create($reliability_list,"reliability_id","class",$route->POST('reliability'));
				?></select>
			</td>
		</tr>
		<tr>
			<td><label for='order'>Приоритет</label></td>
			<td>
				<input type='text' name='order' id='order' class='short' value='<?php echo $route->POST('order'); ?>' /> <span class='gray'>Влияет на порядок размещения в меню</span>
			</td>
		</tr>
		<tr>
			<td><label for='donor'>Страница-донор</label></td>
			<td>
				<input type='text' name='donor' id='donor' value='<?php echo $route->POST('donor'); ?>' />
			</td>
		</tr>
		<tr>
			<td><label for='cookie'>Cookie</label></td>
			<td>
				<input type='text' name='cookie' id='cookie' value='<?php echo $route->POST('cookie'); ?>' />
			</td>
		</tr>
		<tr>
			<td><label for='about'>О компании</label></td>
			<td><textarea name='about' id='about'><?php echo $route->POST('about'); ?></textarea></td>
		</tr>
		<tr>
			<td>Предлагает</td>
			<td>
				<table id="services_list">
					<tr><td><input type='button' id='add_service' value="Добавить услугу" /></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Добавить' /></td>
		</tr>
	</table>
	<select id='hidden_select' style='display:none'><?php
		$services = $db->getTable("select `service_id`,`name` from `services` order by `service_id`");
		echo select_create($services,"service_id","name");
	?></select>
</form>

<?php loadPage("footer.php"); ?>